<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Controller;
use think\Request;
use app\common\model\Article as ArticleModel;
use app\common\model\Category;

class Article extends AdminBase
{
    private static $ArticleModel = null;
    private static $CateModle = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (self::$ArticleModel == null) {
            self::$ArticleModel = new ArticleModel();
        }
        if (static::$CateModle == null) {
            self::$CateModle = new Category();
        }
    }

    /**
     * 正常文章页面
     * @return mixed
     */
    public function lists()
    {
        $get = $_GET;
        unset($get['/admin/article/lists']);
        $where=[];
        if (isset($get['title'])&&!empty($get['title'])){
            $where['a.title']=['like','%'.trim($get['title']).'%'];
        }
        if (isset($get['cateid'])&&!empty($get['cateid'])){
            $where['a.cate_id']=trim($get['cateid']);
        }
        $CateData = self::$CateModle->where('status', 1)->select();
        $this->assign('CateData', $CateData);
        $articleData = self::$ArticleModel->getAllData($where,$get);
        $this->assign('articleData', $articleData);
        return $this->fetch();
    }

    /**
     * 回收站页面
     * @return mixed
     */
    public function recycle(){
        $get = $_GET;
        unset($get['/admin/article/recycle']);
        $where=[];
        if (isset($get['title'])&&!empty($get['title'])){
            $where['a.title']=['like','%'.trim($get['title']).'%'];
        }
        if (isset($get['cateid'])&&!empty($get['cateid'])){
            $where['a.cate_id']=trim($get['cateid']);
        }
        $CateData = self::$CateModle->where('status', 1)->select();
        $this->assign('CateData', $CateData);
        $articleData = self::$ArticleModel->getAllrecycle($where,$get);
        $this->assign('articleData', $articleData);
        return $this->fetch();
    }

    /**
     * 草稿箱界面
     * @return mixed
     */
    public function draft(){
        $get = $_GET;
        unset($get['/admin/article/draft']);
        $where=[];
        if (isset($get['title'])&&!empty($get['title'])){
            $where['a.title']=['like','%'.trim($get['title']).'%'];
        }
        if (isset($get['cateid'])&&!empty($get['cateid'])){
            $where['a.cate_id']=trim($get['cateid']);
        }
        $CateData = self::$CateModle->where('status', 1)->select();
        $this->assign('CateData', $CateData);
        $articleData = self::$ArticleModel->getAlldraft($where,$get);
        $this->assign('articleData', $articleData);
        return $this->fetch();
    }
    /**
     * 清空回收站
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allDel(){
        if (\request()->isAjax()){
            $data = self::$ArticleModel->where('is_recycle',0)->select();
            if ($data==null){
                return $this->resMessage(0,'回收站没有数据');
            }
            $res= self::$ArticleModel->where('is_recycle',0)->delete();
            if ($res){
                return $this->resMessage(1,'操作成功');
            }else{
                return $this->resMessage(0,'操作失败');
            }
        }else{
            abort(404);
        }
    }

    /**
     * 文章添加操作
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (\request()->isGet()) {
            $CateData = self::$CateModle->where('status', 1)->select();
            $this->assign('CateData', $CateData);
            return $this->fetch();
        }
        if (\request()->isAjax()) {
            $post = input('post.');
            $validate = validate('Article');
            if (!$validate->check($post)) {
                return $this->resMessage(0, $validate->getError());
            }
            $articleData = self::$ArticleModel->where('title', $post['title'])->where('cate_id', $post['cate_id'])->find();
            if ($articleData != null) {
                return $this->resMessage(0, '文章已经存在');
            }
            $data = [
                'title' => $post['title'],
                'cate_id' => $post['cate_id'],
                'content' => $post['content'],
                'create_time' => time(),
                'describe' => $post['describe'],
                'update_time' => time(),
                'is_draft'=>$post['draft']
            ];
            if (isset($post['status'])&&$post['status']=='on'){
                $data['status']=1;
            }else{
                $data['status']=0;
            }
            $res = self::$ArticleModel->save($data);
            if ($res) {
                return $this->resMessage(1, '添加成功');
            } else {
                return $this->resMessage(0, '添加失败');
            }

        }

    }

    /**
     * 文章编辑操作
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (\request()->isGet()) {
            if (input('?param.id')) {
                $id = input('param.id');
                $articleData = self::$ArticleModel->get($id);
                $this->assign('articleData', $articleData);
                $CateData = self::$CateModle->where('status', 1)->select();
                $this->assign('CateData', $CateData);
                return $this->fetch();
            } else {
                abort(404);
            }

        }
        if (\request()->isAjax()) {
            $post = input('post.');
            if (!isset($post['id']) && empty($post['id'])) {
                return $this->resMessage(0, '缺少参数');
            } else {
                $id = $post['id'];
            }
            $validate = validate('Article');
            if (!$validate->check($post)) {
                return $this->resMessage(0, $validate->getError());
            }
            $oldData = self::$ArticleModel->get($id);
            if ($oldData == null) {
                return $this->resMessage(0, '数据不存在');
            }
            $data = [
                'title' => $post['title'],
                'cate_id' => $post['cate_id'],
                'content' => $post['content'],
                'describe' => $post['describe'],
                'update_time' => time(),
            ];
            if (isset($post['status'])&&$post['status']=='on'){
                $data['status']=1;
            }else{
                $data['status']=0;
            }
            if (isset($post['draft'])&&!empty($post['draft'])){
                $data['is_draft']=$post['draft'];
            }
            $res = self::$ArticleModel->save($data, ['article_id' => $id]);
            if ($res) {
                return $this->resMessage(1, '操作成功');
            } else {
                return $this->resMessage(0, '操作失败');
            }
        }
    }

    /**
     * 删除数据库文章
     * @return array
     * @throws \think\exception\DbException
     */
    public function del()
    {
        if (\request()->isAjax()){
            if (input('?post.id')){
                $id = input('post.id');
                $oldData = self::$ArticleModel->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $res = self::$ArticleModel->where('article_id',$id)->delete();
                if ($res){
                    return $this->resMessage(1,'删除成功');
                }else{
                    return $this->resMessage(0,'删除失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }else{
            abort(404);
        }
    }

    /**
     * 将文章删除到回收站
     * @return array
     * @throws \think\exception\DbException
     */
    public function delrecycle()
    {
        if (\request()->isAjax()){
            if (input('?post.id')){
                $id = input('post.id');
                $oldData = self::$ArticleModel->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $res = self::$ArticleModel->save(['is_recycle'=>0],['article_id'=>$id]);
                if ($res){
                    return $this->resMessage(1,'删除成功');
                }else{
                    return $this->resMessage(0,'删除失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }else{
            abort(404);
        }
    }

    /**
     * 更改文章状态
     * @return array
     * @throws \think\exception\DbException
     */

    public function upSta()
    {
        if (\request()->isAjax()) {
            if (input('?post.id') && input('?post.sta')) {
                $id = input('post.id');
                $sta = input('post.sta');
                $oldData = self::$ArticleModel->get($id);
                if ($oldData == null) {
                    return $this->resMessage(0, '数据不存在');
                }
                $res = self::$ArticleModel->save(['status' => !$sta], ['article_id' => $id]);
                if ($res) {
                    return $this->resMessage(1, '操作成功');
                } else {
                    return $this->resMessage(0, '操作失败');
                }
            } else {
                return $this->resMessage(0, '缺少参数');
            }
        } else {
            abort(404);
        }
    }

    /**
     * 还原文章
     * @return array
     * @throws \think\exception\DbException
     */
    public function recovery(){
        if (\request()->isAjax()){
            if (input('?post.id')){
                $id = input('post.id');
                $oldData = self::$ArticleModel->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $res = self::$ArticleModel->save(['is_recycle'=>1],['article_id'=>$id]);
                if ($res){
                    return $this->resMessage(1,'操作成功');
                }else{
                    return $this->resMessage(0,'操作失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }else{
            abort(404);
        }
    }


}
